/**
 * 作弊筛选
 * @author Peter
 *
 */
angular.module('imsApp').controller('cheatBatchController', ['$scope', '$rootScope', '$state','$filter', 'cheatBatchManager','FocusManager','DisallowManager','$sce','ImsDialog',
    function ($scope, $rootScope, $state, $filter,cheatBatchManager,FocusManager,DisallowManager,$sce,ImsDialog) {
        //Search data
        $scope.search = {method:$state.params.method||'cuid',
            startDay: $state.params.startDay || null,
            endDay: $state.params.endDay || null,
            status:$state.params.status||0,type:$state.params.type,pageNo:$state.params.pageNo || 1,pageSize:$state.params.pageSize||10};

        //Init
        $scope.status=[500,501,502];
        $scope.types=$scope.search.method=='ip'?[411,412]:[413,414];//[411,412,413,414];


        //Page
        $scope.paginationConf = {
            currentPage: $scope.search.pageNo,
            itemsPerPage: $scope.search.pageSize,
            pagesLength: 9,
            search: false,
            onChange: function(){
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search);
            }
        };

        //搜索公共部分
        var searchGo = function(){
            $scope.search.pageNo = 1;
            $scope.search.pageSize = 10;
            $state.go($state.current.name,$scope.search);//第一个参数是当前路由，第二个参数是传的参数
        };

        //Search
        $scope.searchClick = function(){
            searchGo();
        };

        //Choose date
        $scope.rangeDate = {
            startDate:$scope.search.startDay ? moment($scope.search.startDay).format('X'):null,
            endDate:$scope.search.endDay? moment($scope.search.endDay).format('X'):null,
            onSelected: function (data, scope) {  //submit
                $scope.search.startDay =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
                $scope.search.endDay = data.endDate?moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;
                searchGo();
            }
        };


        //Get data
        if($scope.search.method =='ip'){
            //Get IP list
            cheatBatchManager.cheatBatchIps([$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
                $scope.lists = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            })
        }else{
            //Get ID list
            cheatBatchManager.cheatBatchCuids([$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
                $scope.lists = result.data.pageList;
                $scope.paginationConf.totalItems = result.data.total;
            })
        }

        //初始化全选值为false
        $scope.checkData={allChecked:false}
        //全选
        $scope.selectAllChange=function (checked) {
            $scope.lists.forEach(function (item) {
                item.checked=checked;
            })
        }

        //选择一个
        $scope.selectOneChange=function (checked) {
            if(!checked){
                $scope.checkData.allChecked=false;
            }else {
                ($filter('filter')($scope.lists,{checked:true}).length)==$scope.lists.length?$scope.checkData.allChecked=true:$scope.checkData.allChecked=false;
            }
        }

        /**
         * Add Focus
         * @param item
         */
        $scope.addFocus = function(item){
            if($scope.search.method=='ip'){
                FocusManager.ipAdd({list:[[item.ip,item.type]]}).then(function (result) {
                    if (result.data.c == "s") {
                        $state.reload();
                    }
                })
            }else{
                FocusManager.cuidAdd({list:[[item.cuid,item.type]]}).then(function (result) {
                    if (result.data.c == "s") {
                        $state.reload();
                    }
                })
            }
        };

        /**
         * Add Disallow
         * @param item
         */
        $scope.addDisallow = function(item){
            if($scope.search.method=='ip'){
                DisallowManager.ipAdd({list:[[item.ip,item.type]]}).then(function (result) {
                    if (result.data.c == "s") {
                        $state.reload();
                    }
                })
            }else{
                FocusManager.cuidAdd({list:[[item.cuid,item.type]]}).then(function (result) {
                    if (result.data.c == "s") {
                        $state.reload();
                    }
                })
            }
        };

        /**
         * Choose IP or ID
         * @param val
         */
        $scope.filterClick = function(val){
            $scope.search.method = val;
            $scope.search.status=0;
            $scope.search.type=0;
            searchGo();
        }

        /**
         * Forbide all (IP/cuid)
         */
        $scope.ForbideAll = function(){
            var tempArray = [];

            $scope.lists.forEach(function(item){
                if(item.checked==true){
                    if(item.status != 501)
                        tempArray.push([item.ip?item.ip:item.cuid,item.type]);
                }
            });
            if(tempArray.length>0){
                if($scope.search.method=='ip'){
                    DisallowManager.ipAdd({list:tempArray}).then(function (result) {
                        if(result.data.c=='s'){
                            $state.reload();
                        }
                    });
                }else{
                    DisallowManager.cuidAdd({list:tempArray}).then(function (result) {
                        if(result.data.c=='s'){
                            $state.reload();
                        }
                    });
                }
            }else{
                ImsDialog.open({title:"作弊筛选",message:"没有可封禁的数据。"}).then(function (a) {
                })
            }

        };

        /**
         * Focus all (IP/cuid)
         *
         */
        $scope.FocusAll = function(){
            var tempArray = [];
            var disAllowNum = 0;
            var disAllowArray = [];
            $scope.lists.forEach(function(item){
                if(item.checked==true){
                    if(item.status==501){
                        disAllowArray.push(item);
                        disAllowNum++;
                    }else if(item.status==502){
                        tempArray.push([item.ip?item.ip:item.cuid,item.type]);
                    }

                }
            });
            if(tempArray.length>0){
                if($scope.search.method=='ip'){
                    FocusManager.ipAdd({list:tempArray}).then(function (result) {
                        if(result.data.c=='s'){
                            if(disAllowNum){
                                ImsDialog.open({title:"作弊筛选",message:"您有"+disAllowNum+"条数据关注失败（原因：已封禁的数据不能关注）"}).then(function (a) {

                                })
                            }

                            $state.reload();
                        }
                    });
                }else{
                    FocusManager.cuidAdd({list:tempArray}).then(function (result) {
                        if(result.data.c=='s'){
                            if(disAllowNum){
                                ImsDialog.open({title:"作弊筛选",message:"您有"+disAllowNum+"条数据关注失败（原因：已封禁的数据不能关注）"}).then(function (a) {

                                })
                            }

                            $state.reload();
                        }
                    });
                }
            }else{
                ImsDialog.open({title:"作弊筛选",message:"没有可关注的数据。"}).then(function (a) {
                })
            }

        };

    }]);

